//
//  ViewController.m
//  UI16_UICollectionViewHomeWork
//
//  Created by dllo on 16/3/30.
//  Copyright © 2016年 XWH. All rights reserved.
//

#import "ViewController.h"
#import "ViewOfLayout.h"
#import "ViewOfCollection.h"
#import "Header.h"
#import "CellOfCollection.h"
#import "ModelOfPicture.h"
#import "ModelOfPicture.h"
#import "RootViewController.h"
@interface ViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, strong) ViewOfLayout *flowLaout;
@property (nonatomic, strong) ViewOfCollection *collection;
@property (nonatomic, strong) NSMutableArray *arrOfPicture;
@property (nonatomic, strong) ModelOfPicture *model;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatCollectionView];
    [self handleData];
}

#pragma mark - flowLaout

-(void)creatLayoutView{
    
    self.flowLaout = [[ViewOfLayout alloc] init];
    
}

#pragma mark - collection

-(void)creatCollectionView{
    [self creatLayoutView];
    self.collection = [[ViewOfCollection alloc] initWithFrame:SCREEN_BOUNDS collectionViewLayout:self.flowLaout];
    
    self.collection.delegate = self;
    self.collection.dataSource = self;
    
    [self.collection registerClass:[CellOfCollection class] forCellWithReuseIdentifier:@"pool"];
    
    [self.view addSubview:self.collection];
    
}

#pragma mark - hadlerData

-(void)handleData{
    self.arrOfPicture = [NSMutableArray arrayWithCapacity:0];
    
    for (int i = 0; i < 18; i++) {
        NSString *imageName = [NSString stringWithFormat:@"%02d.jpg", i];
        
        UIImage *image = [UIImage imageNamed:imageName];
        
        [self.arrOfPicture addObject:image];
        
        
       self.model  = [[ModelOfPicture alloc] init];
        
        [self.model setValue:self.arrOfPicture forKey:@"picName"];
       
    }
    

}


#pragma mark - delegate

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.arrOfPicture.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CellOfCollection *cell = [self.collection dequeueReusableCellWithReuseIdentifier:@"pool" forIndexPath:indexPath];
    
    
    cell.backgroundColor = [UIColor orangeColor];
    
//    ModelOfPicture *model = self.arrOfPicture[indexPath.row];
    
    cell.imageView.image = self.arrOfPicture[indexPath.item];
    
//    NSLog(@"%@", self.arrOfPicture[indexPath.row]);
    return cell;
    
    
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    RootViewController *root = [[RootViewController alloc] init];
    [self.navigationController pushViewController:root animated:YES];
    
    root.model = self.model;
    root.array = self.arrOfPicture;
    root.index = indexPath;
//    root.number = self.arrOfPicture.count;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
